<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;
use App\Models\Scene;
use Log;
use App\Models\Course;
use Encore\Admin\Form;
use GuzzleHttp\Client;

class AddController extends Controller
{
    public function add()
    {
        $randomNum = $this->generateRandomString(6);
        if (Admin::user()) {
            $data = Admin::user()->courses;
            // dd($data);
            $courses = DB::table('courses');
            $courses = $courses->get();
            // dd($courses);
            $num = $data->count();
            // return view('welcome',['data'=>$data],);
            Log::info($data);
            return view('add', compact('data', 'num', 'courses', 'randomNum'));
        } else {
            return redirect('admin/auth/login');
        }
    }
    // 快速建馆
    public function addQuickly()
    {
        $randomNum = $this->generateRandomString(6);
        if (Admin::user()) {
            $courses = DB::table('courses');
            $courses = $courses->get();
            $num=$courses->count();
            // dd($courses);
            // dd($num);
            // return view('welcome',['data'=>$data],);
            //Log::info($data);
            // dd($data);
            return view('addQuickly', compact('num', 'courses', 'randomNum'));
        } else {
            return redirect('admin/auth/login');
        }
    }
    // 快速基于模板建馆 course
    public function addQuicklyDetail($id){
        $randomNum = $this->generateRandomString(6);
        if (Admin::user()) {

            $scenes = DB::table('scenes');
            $scenes = $scenes->get();
            // $thisScene=DB::table('scenes')->where('id','$id')->first();
            $thisScene=$scenes->where('id',(int)$id)->first();
            // var_dump($thisScene);
            $num=$scenes->count();
            // dd($id);
            // dd($thisScene);
            $post=0;
            // dd($num);
            // return view('welcome',['data'=>$data],);
            //Log::info($data);
            // dd($data);
            return view('addQuicklyDetail', compact('num', 'scenes', 'randomNum','post','id','thisScene'));
        } else {
            return redirect('admin/auth/login');
        }
    }
    // 删除课程
    public function del($id)
    {
        $course = Course::findOrFail($id);
        $course->delete();
        return back();
    }


    public function form(Request $request)
    {
        $input = $request->all();
        // 选中的模板的id
        $scene_id = $request->Input('scene_id');
        $scene = Scene::find($scene_id);
        $client = new Client();
        $url = url('tp5/public/index.php/index/gg/createCourse');
        $response = $client->request('GET', $url, [
            'headers' => [
                'token' => session('token')
            ],
            'query' => [
                'sceneNum' => $scene->sceneNum,
                'courseName' => $request->Input('sceneName'),
                'courseNum' => $request->Input('sceneNum')
            ]
        ]);
        $course = new Course($scene->attributesToArray());
        $course->scene_id = $request->Input('scene_id');
        $course->sceneNum = $request->Input('sceneNum');
        $course->sceneName = $request->Input('sceneName');;
        $course->create_user = Admin::user()->id;
        $course->save();
        $randomNum = $this->generateRandomString(6);
        $data = Admin::user()->courses;
        $scenes = DB::table('scenes');
        $scenes = $scenes->get();
        // dd($scenes);
        $num = $data->count();
        $post = 1;
        //return view('add', compact('data', 'num', 'scenes', 'randomNum','post'));
        return redirect('add');
    }

    private function generateRandomString($length = 10)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
}
